package com.simple3d.primitives
{
	import flash.display.BitmapData;
	import flash.display.Sprite;
	import flash.geom.Matrix3D;
	import flash.geom.Utils3D;
	import flash.geom.Vector3D;
	
	/**
	 * The Plane class is used to create a simple Plane. 
	 * @author Pradeek
	 */	
	public class Plane extends Sprite
	{
		private var displayMode:uint = 0;
		
		protected var vertices:Vector.<Number>;
		protected var projectedVertices:Vector.<Number>;
		protected var indices:Vector.<int>;
		protected var uvtData:Vector.<Number>;
		protected var matrix3d:Matrix3D;
		
		/**
		 *   Indicates the width of the plane, in pixels.
		 */		
		public var _width:Number = 100;
		
		/**
		 *   Indicates the width of the plane, in pixels.
		 */
		public var _height:Number = 100;
		
		/**
		 *   Indicates the color of the plane.
		 */
		public var color:uint = 0x000000;
		
		/**
		 *   Indicates the BitmapData of the texture for the plane.
		 */
		public var texture:BitmapData;
		
		/**
		 * Constructor
		 * @param _width - Width of the plane.
		 * @param _height - Height 0f the plane
		 * @param texture - BitmapData of the texture.
		 */		
		public function Plane(_width:Number, _height:Number,texture:BitmapData = null)
		{
			this._width = _width;
			this._height = _height;
			this.texture = texture;
			
			if(texture)
			{
				displayMode = 1;
			}
			
			vertices = new Vector.<Number>();
			projectedVertices = new Vector.<Number>();
			indices = new Vector.<int>();
			uvtData = new Vector.<Number>();			
			matrix3d = new Matrix3D();
			
			buildPlane();
		}
		
		private function buildPlane():void
		{
			vertices.push(0,0,0);
			vertices.push(this._width,0,0);
			vertices.push(this._width,this._height,0);
			vertices.push(0,this._height,0);
			
			indices.push(0,1,2);
			indices.push(2,3,0);								
							
			uvtData.push(0,0,0);
			uvtData.push(1,0,0);
			uvtData.push(1,1,0);
			uvtData.push(0,1,0);			
		}
		
		/**
		 *  Renders the plane. 
		 */		
		public function render():void
		{
			Utils3D.projectVectors(matrix3d,vertices,projectedVertices,uvtData);
			
			this.graphics.clear();
			switch(displayMode)
			{
				case 0:
					this.graphics.beginFill(color);
					this.graphics.lineStyle(1,color);
					this.graphics.drawTriangles(projectedVertices,indices);
					break;
				case 1:
					this.graphics.beginBitmapFill(this.texture,null,false,false);
					this.graphics.drawTriangles(projectedVertices,indices,uvtData);
					break;
			}
			this.graphics.endFill();			
		}
		
		/**
		 *  Rotate the 3D object on all axes.
		 *  @param value - amount of rotation.
		 */		
		public function rotate(value:Number):void
		{  
			matrix3d.appendRotation(value,Vector3D.X_AXIS);
			matrix3d.appendRotation(value,Vector3D.Y_AXIS);
			matrix3d.appendRotation(value,Vector3D.Z_AXIS);
		}		
		
		public override function set rotationX(value:Number):void
		{
			matrix3d.appendRotation(value,Vector3D.X_AXIS);
		}
		
		public override function set rotationY(value:Number):void
		{
			matrix3d.appendRotation(value,Vector3D.Y_AXIS);
		}
		
		public override function set rotationZ(value:Number):void
		{
			matrix3d.appendRotation(value,Vector3D.Z_AXIS);
		}

	}
}